{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "complicated-sunday",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/\n",
    "\n",
    "\n",
    "Runtime: 8 ms, faster than 32.69% of C++ online submissions for Flatten a Multilevel Doubly Linked List.\n",
    "Memory Usage: 8.9 MB, less than 13.83% of C++ online submissions for Flatten a Multilevel Doubly Linked List.\n",
    "\n",
    "\n",
    "```cpp\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <string>\n",
    "#include <deque>\n",
    "#include <iostream>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "class Node {\n",
    "public:\n",
    "    int val;\n",
    "    Node* prev;\n",
    "    Node* next;\n",
    "    Node* child;\n",
    "};\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    Node* flatten(Node* head) {\n",
    "        //10:01\n",
    "        vector<Node*> linkList;\n",
    "\n",
    "        deque<Node*> q;\n",
    "        q.push_front(head);\n",
    "        Node* node;\n",
    "        while (q.size()) {\n",
    "            node = q.front();\n",
    "            q.pop_front();\n",
    "            if (node != nullptr) {\n",
    "                if (node->next != nullptr) {\n",
    "                    q.push_front(node->next);\n",
    "                }\n",
    "                if (node->child != nullptr) {\n",
    "                    q.push_front(node->child);\n",
    "                }\n",
    "                linkList.push_back(node);\n",
    "            }\n",
    "        }\n",
    "\n",
    "        for (int i=0; i<linkList.size(); i++) {\n",
    "            node = linkList[i];\n",
    "            if (i == 0) {\n",
    "                node->prev = nullptr;\n",
    "                node->next = linkList[i+1];\n",
    "            } else if (i == linkList.size()-1) {\n",
    "                node->prev = linkList[i-1];\n",
    "                node->next = nullptr;\n",
    "            } else {\n",
    "                node->prev = linkList[i-1];\n",
    "                node->next = linkList[i+1];\n",
    "            }\n",
    "            node->child = nullptr;\n",
    "            //cout << node->val << endl;\n",
    "        }\n",
    "\n",
    "        return head;\n",
    "        //10:31 done; 11:15 debug\n",
    "    }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "healthy-wiring",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
